import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { UserService } from 'src/app/common/providers';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less'],
})
export class LoginComponent implements OnInit {
  // 登录响应式表单
  validateForm: FormGroup = this.fb.group({
    loginName: ['admin', Validators.required],
    password: ['123456', Validators.required],
  });
  // 控制密码是否可见
  showPassword = false;
  // 控制登录按钮的加载状态
  hasClicked = false;

  constructor(
    private router: Router,
    private fb: FormBuilder,
    private userServ: UserService
  ) {}

  ngOnInit(): void {}

  submitForm(): void {
    this.hasClicked = true;
    if (this.validateForm.valid) {
      this.userServ.login(this.validateForm.value).subscribe(
        (res) => {
          this.hasClicked = false;
          if (res.success) {
            this.router.navigateByUrl('/');
          }
        },
        (err) => {
          this.hasClicked = false;
        }
      );
    } else {
      this.hasClicked = false;
    }
  }
}
